<template>
  <div>
    <div>
      <el-container>
        <el-header class="homeHeader">
          <div class="title">大数据项目管理系统</div>
          <!-- 头像 -->
          <div>
            <el-dropdown class="userInfo" @command="commandHandler">
              <span class="el-dropdown-link">
                <i> <img :src="user.userFace" /> </i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>个人中心</el-dropdown-item>
                <el-dropdown-item>设置</el-dropdown-item>
                <el-dropdown-item command="logout">注销登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-container>
          <el-aside width="200px">
            <el-menu @select="menuClick">
              <el-submenu index="1">
                <template slot="title"
                  ><i class="el-icon-user-solid"></i>用户管理</template
                >
                <el-menu-item index="/usermanager">用户详细</el-menu-item>
              </el-submenu>
              <el-submenu index="2">
                <template slot="title"
                  ><i class="el-icon-tickets"></i>项目管理</template
                >
                <el-menu-item index="/projectmanage">项目详细</el-menu-item>
                <el-menu-item index="/projectcomment">项目评论</el-menu-item>
              </el-submenu>
              <el-submenu index="3">
                <template slot="title"
                  ><i class="el-icon-s-management"></i>实验管理</template
                >
                <el-menu-item index="/expmanager">实验详细</el-menu-item>
                <el-menu-item index="/expcomment">实验评论</el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
          <el-main>
            <el-breadcrumb
              separator-class="el-icon-arrow-right"
              v-if="this.$router.currentRoute.path != '/home'"
            >
              <el-breadcrumb-item :to="{ path: '/home' }"
                >首页</el-breadcrumb-item
              >
              <el-breadcrumb-item>{{
                this.$router.currentRoute.name
              }}</el-breadcrumb-item>
            </el-breadcrumb>
            <div
              class="homeWelcome"
              v-if="this.$router.currentRoute.path == '/home'"
            >
              欢迎来到小学期大数据管理系统
            </div>
            <router-view />
          </el-main>
        </el-container>
      </el-container>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      user: JSON.parse(window.sessionStorage.getItem('user')),
    }
  },
  methods: {
    menuClick(index) {
      this.$router.push(index)
    },
    commandHandler(command) {
      if (command == 'logout') {
        this.$confirm('此操作将注销登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            // 注销登录
            this.getRequest('/user/logout').then((resp) => {
              if (resp) {
                if (resp.code == 200) {
                  // 销毁用户的session
                  window.sessionStorage.removeItem('user')
                  this.$router.push({ path: '/' })
                }
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作',
            })
          })
      }
    },
    initRole() {
      if (!window.sessionStorage.getItem('user')) {
        this.$message.error('请先进行登录!')
        this.$router.push({ path: '/' })
      }

      var a = JSON.parse(window.sessionStorage.getItem('user'))

      if (a.roleType == 1 || a.roleType == 2) {
        this.$message.error('权限不足!')
        this.$router.push({ path: '/' })
      }
    },
  },
  created() {
    this.initRole()
  },
}
</script>

<style>
.homeHeader {
  background: #409eff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}

.title {
  font-size: 30px;
  font-family: 华文楷体;
  color: white;
}

.userInfo {
  cursor: pointer;
}
.el-dropdown-link img {
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 8px;
}
.homeWelcome {
  text-align: center;
  font-size: 30px;
  font-family: 华文楷体;
  color: #409eff;
  padding-top: 50px;
}
.homeRouterView {
  margin-top: 10px;
}
</style>